package com.baitiaojun.api.util;

import com.baitiaojun.api.exception.ConditionException;
import com.coremedia.iso.IsoFile;
import com.coremedia.iso.boxes.MovieHeaderBox;
import com.googlecode.mp4parser.DataSource;
import com.googlecode.mp4parser.FileDataSourceImpl;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;

/**
 * @类名 FileUtil
 * @描述 TODO
 * @作者 白条君
 * @创建日期 2022/12/9 21:50
 * @版本 1.0
 */
public class FileUtil {
    
    public static String getFileName(String filePath) {
        int index = filePath.lastIndexOf("/");
        return filePath.substring(index + 1);
    }

    public static String getType(MultipartFile file) throws ConditionException {
        String originalFilename = file.getOriginalFilename();
        if (ObjectUtils.isEmpty(originalFilename)) {
            throw new ConditionException("文件不能为空");
        }
        int index = originalFilename.lastIndexOf(".");
        return originalFilename.substring(index + 1);
    }

    public static boolean deleteFileByMd5(String md5) throws ConditionException {
        File file = new File(ConstantUtil.TEMP_FILE_SLICE_PATH);
        File[] files = file.listFiles();
        boolean result = false;
        for (File file1 : files) {
            if (file1.getName().equals(".DS_Store")) {
                continue;
            }
            result = delete(md5, file1);
            if (result) {
                break;
            }
        }
        return result;
    }

    private static boolean delete(String md5, File file) throws ConditionException {
        boolean result = false;
        if (ObjectUtils.isNotEmpty(file)) {
            for (File file1 : file.listFiles()) {
                if (ObjectUtils.isNotEmpty(file1) && file1.getName().equals(md5)) {
                    for (File listFile : file1.listFiles()) {
                        boolean delete = listFile.delete();
                        if (!delete) {
                            throw new ConditionException("文件删除失败");
                        }
                    }
                    return file1.delete();
                }
            }
        }
        return result;
    }

    public static String getDuration(String localPath) throws Exception {
        //获取文件url
        IsoFile isoFile = new IsoFile(localPath);
        MovieHeaderBox movieHeaderBox = isoFile.getMovieBox().getMovieHeaderBox();
        long lengthInSeconds = movieHeaderBox.getDuration() / movieHeaderBox.getTimescale() + 1;
        String length;
        DecimalFormat decimalFormat = new DecimalFormat("00");
        if (lengthInSeconds < 3600) {
            int minus = (int) lengthInSeconds / 60;
            int seconds = (int) lengthInSeconds - minus * 60;
            length = decimalFormat.format(minus) + ":" + decimalFormat.format(seconds);
        } else {
            int hour = (int) lengthInSeconds / 60 / 60;
            int minus = (int) lengthInSeconds / 60 - 60 * hour;
            int seconds = (int) lengthInSeconds - hour * 60 * 60 - minus * 60;
            length = decimalFormat.format(hour) + ":" + decimalFormat.format(minus) + ":" + decimalFormat.format(seconds);
        }
        return length;
    }

    public static void deleteFile(String fileName) throws ConditionException {
        File file = new File(ConstantUtil.TEMP_FILE_DOWNLOAD_PATH);
        File[] files = file.listFiles();
        for (File tempFile : files) {
            if (tempFile.getName().equals(fileName)) {
                boolean delete = tempFile.delete();
                if (!delete) {
                    throw new ConditionException("临时文件删除失败");
                }
            }
        }
    }

    public static Map<String, String> splitFilePath(String filePath) throws Exception {
        int index = filePath.indexOf("/");
        String group = filePath.substring(0, index);
        String path = filePath.substring(index + 1);
        Map<String, String> filePathMap = new HashMap<>();
        filePathMap.put("group", group);
        filePathMap.put("filePath", path);
        return filePathMap;
    }

    public static String getfileExtName(String originName) {
        int index = originName.lastIndexOf(".");
        return originName.substring(index + 1);
    }
}
